import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:kunlun_gas/app_files.dart';
import 'action.dart';
import 'state.dart';

Widget buildView(
    MachineOrderViewState state, Dispatch dispatch, ViewService viewService) {
  Widget _buildItem(BuildContext context, int index) {
    MachineOrderItemModel model = state.items[index];
    double charge = double.tryParse(model.chargeFee) ?? 0.0;
    return Container(
      padding: EdgeInsets.only(left: 16, right: 16),
      color: Colors.white,
      child: Container(
        padding: EdgeInsets.only(top: 10, bottom: 14),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Expanded(
                    child: Text(
                  charge != 0 ? '充值金额' : '用气金额',
                  style: TextThemeConfig.Font11WithColor99,
                )),
                charge != 0
                    ? Text(
                        '+¥${model.chargeFee}',
                        style: TextThemeConfig.Font16WithMainColor,
                      )
                    : Text(
                        '-¥${model.orderMoney}',
                        style: TextThemeConfig.Font16WithColorRed,
                      ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 6)),
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Expanded(
                    child: Text(
                  '用气量',
                  style: TextThemeConfig.Font11WithColor99,
                )),
                Text(
                  charge != 0 ? '-' : '${model.orderGas}',
                  style: TextThemeConfig.Font14WithColor66,
                ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 6)),
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Expanded(
                    child: Text(
                  charge != 0 ? '充值日期' : '扣款日期',
                  style: TextThemeConfig.Font11WithColor99,
                )),
                Text(
                  '${model.orderTime}',
                  style: TextThemeConfig.Font14WithColor66,
                ),
              ],
            ),
          ],
        ),
        decoration: index == state.items.length - 1
            ? null
            : BoxDecoration(
                border: Border(
                  bottom: BorderSide(color: ThemeConfig.lineColor),
                ),
              ),
      ),
    );
  }

  return Scaffold(
    backgroundColor: Colors.white,
    appBar: myAppBar(viewService.context, title: '扣款记录'),
    body: state.model == null
        ? MyLoadingWidget()
        : Container(
            color: ThemeConfig.separeColor,
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.symmetric(vertical: 10),
                  color: Colors.white,
                  padding: EdgeInsets.only(top: 8, bottom: 8),
                  child: Row(
                    children: [
                      Expanded(
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset('images/nb_balance.png'),
                                Padding(padding: EdgeInsets.only(left: 4)),
                                Text(
                                  state.model.balance,
                                  style: TextThemeConfig.Font15WithBigColor22,
                                ),
                              ],
                            ),
                            Text(
                              '账户余额',
                              style: TextThemeConfig.Font13WithColor78,
                            )
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset('images/nb_point.png'),
                                Padding(padding: EdgeInsets.only(left: 4)),
                                Text(
                                  state.model.lastPointNumber,
                                  style: TextThemeConfig.Font15WithBigColor22,
                                ),
                              ],
                            ),
                            Text(
                              '抄表指针',
                              style: TextThemeConfig.Font13WithColor78,
                            )
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset('images/nb_arrears.png'),
                                Padding(padding: EdgeInsets.only(left: 4)),
                                Text(
                                  state.model.arrearsFee,
                                  style: TextThemeConfig.Font15WithBigColor22,
                                ),
                              ],
                            ),
                            Text(
                              '欠款金额',
                              style: TextThemeConfig.Font13WithColor78,
                            )
                          ],
                        ),
                      ),
                      Expanded(
                        child: Column(
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Image.asset('images/nb_later.png'),
                                Padding(padding: EdgeInsets.only(left: 4)),
                                Text(
                                  state.model.lateFee,
                                  style: TextThemeConfig.Font15WithBigColor22,
                                ),
                              ],
                            ),
                            Text(
                              '滞纳金',
                              style: TextThemeConfig.Font13WithColor78,
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  color: Colors.white,
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  child: Column(
                    children: [
                      Container(
                        height: 40,
                        child: Row(
                          children: [
                            Image.asset('images/nb_number.png'),
                            Padding(padding: EdgeInsets.only(left: 5)),
                            Text(
                              '表号',
                              style: TextThemeConfig.Font11WithColor77,
                            ),
                            Expanded(child: Container()),
                            Text(
                              state.model.gasMeterNo,
                              style: TextThemeConfig.Font14WithColor22,
                            )
                          ],
                        ),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom:
                                    BorderSide(color: ThemeConfig.lineColor))),
                      ),
                      Container(
                        height: 40,
                        child: Row(
                          children: [
                            Image.asset('images/nb_meter.png'),
                            Padding(padding: EdgeInsets.only(left: 5)),
                            Text(
                              '表具类型',
                              style: TextThemeConfig.Font11WithColor77,
                            ),
                            Expanded(child: Container()),
                            Text(
                              state.model.meterFactory,
                              style: TextThemeConfig.Font14WithColor22,
                            )
                          ],
                        ),
                        decoration: BoxDecoration(
                            border: Border(
                                bottom:
                                    BorderSide(color: ThemeConfig.lineColor))),
                      ),
                      Container(
                        height: 40,
                        child: Row(
                          children: [
                            Image.asset('images/nb_clock.png'),
                            Padding(padding: EdgeInsets.only(left: 5)),
                            Text(
                              '最后抄表时间',
                              style: TextThemeConfig.Font11WithColor77,
                            ),
                            Expanded(child: Container()),
                            Text(
                              state.model.lastRecordTime,
                              style: TextThemeConfig.Font14WithColor22,
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 16),
                  margin: EdgeInsets.symmetric(vertical: 10),
                  height: 40,
                  color: Colors.white,
                  child: InkWell(
                    child: Row(
                      children: [
                        Image.asset('images/nb_time.png'),
                        Padding(padding: EdgeInsets.only(left: 5)),
                        Text(
                          '业务日期',
                          style: TextThemeConfig.Font11WithColor77,
                        ),
                        Expanded(child: Container()),
                        (state.startTime == null || state.endTime == null)
                            ? Text(
                                '请选择业务日期',
                                style: TextThemeConfig.Font14WithColor99,
                              )
                            : Text(
                                '${state.startTime} 至 ${state.endTime}',
                                style: TextThemeConfig.Font14WithMainColor,
                              ),
                        Padding(
                          padding: EdgeInsets.only(left: 4),
                          child: Image.asset('images/nb_arrow.png'),
                        ),
                      ],
                    ),
                    onTap: () =>
                        dispatch(MachineOrderViewActionCreator.onTapTime()),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: state.items == null
                        ? MyLoadingWidget()
                        : CustomRefreshList(
                            onLoading: (_, page) => dispatch(
                                MachineOrderViewActionCreator.onLoad(page)),
                            onRefresh: (_) => dispatch(
                                MachineOrderViewActionCreator.onLoad(1)),
                            refreshController: state.refreshController,
                            currentPage: state.currentPage,
                            child: state.items.isEmpty
                                ? MyEmptyItemWidget()
                                : ListView.builder(
                                    itemBuilder: _buildItem,
                                    itemCount: state.items.length,
                                    physics: NeverScrollableScrollPhysics(),
                                    shrinkWrap: true,
                                  ),
                          ),
                  ),
                ),
              ],
            ),
          ),
  );
}
